/**
 * 
 */
package org.istt.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.istt.model.FileBean;
import org.istt.model.FileReportBean;
import org.istt.web.WebConstants;

/**
 * @author Ndtho
 *
 */
public class ReportProcessingFiles extends BaseReport{
	
	public InputStream toMSWord(InputStream docFile, List<FileReportBean> lstReport, String reportTitle, String fromDate, String toDate) throws IOException {
	    
	    XWPFDocument docx = new XWPFDocument(docFile);
	    String title = "";
	    if(reportTitle.equals(WebConstants.REPORT_TYPE_PROCESS_NOT_APPROVED)){
	    	title = "CH\u01AFA GI\u1EA2I QUY\u1EBET";
	    }else if(reportTitle.equals(WebConstants.REPORT_TYPE_PROCESS_OVER_TIME)){
	    	title = "\u0110\u00C3 GI\u1EA2I QUY\u1EBET";
	    }
	    String date = "T\u1EEB ng\u00E0y "+fromDate+" \u0111\u1EBFn ng\u00E0y "+toDate ;
	    
	    
		List<XWPFParagraph> paragraphs = docx.getParagraphs();
		for (int i = 0; i < paragraphs.size(); i++)
		{
			XWPFParagraph paragraph = paragraphs.get(i);
			XWPFRun r = paragraph.createRun();
			switch(i)
			{
			  case 0: //Report type
				  r.setText(title); 
				  r.setBold(true);
				  r.setFontFamily(FONT_TIMES_NEW_ROMAN);
				  r.setFontSize(FONT_SIZE_11);
				  break;
			  case 1: //Report type
				  r.setText(date); 
				  r.setItalic(true);
				  r.setFontFamily(FONT_TIMES_NEW_ROMAN);
				  r.setFontSize(FONT_SIZE_11);
				  break;
			}
		}

        /*==================== HEADER ====================*/
	    if(lstReport!=null && lstReport.size()!=0){
	    	for (int i = 0; i < lstReport.size(); i++) {
	    		FileReportBean fileReportBean = (FileReportBean) lstReport.get(i);
	    		generateTableReport(docx,fileReportBean);
	    	}
	    }
        
	    //write to word file
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    docx.write(baos);
	    
	    return new ByteArrayInputStream(baos.toByteArray());
	}
	
	private void generateTableReport(XWPFDocument docx, FileReportBean fileReportBean){
		XWPFParagraph para = docx.createParagraph();
		
		XWPFRun r = para.createRun();
		r.setBold(true);
		r.setFontFamily(FONT_TIMES_NEW_ROMAN); 
		r.setText("PH\u00D2NG: "+fileReportBean.getDepartment().toUpperCase());
		
		//Header
        XWPFTable tableHeader=docx.createTable();
		XWPFTableRow row = tableHeader.getRow(0);
		createTableHeader(row);
		
		List<FileBean> lstFile = fileReportBean.getLstFile();
		if(lstFile.size()!=0){
			int rowcount = 1;
			for (Iterator<FileBean> iterator = lstFile.iterator(); iterator.hasNext();) {
				FileBean fileBean = (FileBean) iterator.next();
				XWPFTableRow content_row = tableHeader.createRow();
				generateRow(fileBean, content_row, rowcount);
				
				rowcount++;
			}
		}
	}
	
	private void generateRow(FileBean fileBean, XWPFTableRow content_row,int rowNo){
		//No
		XWPFRun stt_column = content_row.getCell(0).getParagraphs().get(0).createRun();
		content_row.getCell(0).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(0).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		stt_column.setText(rowNo+"");
		stt_column.setFontSize(FONT_SIZE_10);
		
		// Sender
		XWPFRun sender_column = content_row.getCell(1).getParagraphs().get(0).createRun();
		content_row.getCell(1).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(1).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		sender_column.setText(fileBean.getSubmiter());
		sender_column.setFontSize(FONT_SIZE_10);

		// address: the location of building or address of sender
		XWPFRun location_column = content_row.getCell(2).getParagraphs().get(0).createRun();
		content_row.getCell(2).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(2).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		location_column.setFontSize(FONT_SIZE_10);
		if(fileBean.getLocation()!=null)
			location_column.setText(fileBean.getLocation());
		
		// category
		XWPFRun category_column = content_row.getCell(3).getParagraphs().get(0).createRun();
		content_row.getCell(3).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(3).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		category_column.setFontSize(FONT_SIZE_10);
		category_column.setText(fileBean.getCategory());

		// submit date
		XWPFRun inputDate_column = content_row.getCell(4).getParagraphs().get(0).createRun();
		content_row.getCell(4).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(4).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		inputDate_column.setFontSize(FONT_SIZE_10);
		inputDate_column.setText(fileBean.getInputDate());

		// the date that the processing department receives file
		XWPFRun dateCm_column = content_row.getCell(5).getParagraphs().get(0).createRun();
		content_row.getCell(5).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(5).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		dateCm_column.setFontSize(FONT_SIZE_10);
		dateCm_column.setText(fileBean.getDateReceiveFileProcess());

		// The person who receives the file for processing
		XWPFRun receiverCm_column = content_row.getCell(6).getParagraphs().get(0).createRun();
		content_row.getCell(6).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(6).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		receiverCm_column.setFontSize(FONT_SIZE_10);
		receiverCm_column.setText(fileBean.getWhoReceiveFileProcess());

		// the date returns the file to onegate department.
		XWPFRun dateOnegate_column = content_row.getCell(7).getParagraphs().get(0).createRun();
		content_row.getCell(7).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(7).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		dateOnegate_column.setFontSize(FONT_SIZE_10);
		dateOnegate_column.setText(fileBean.getDateReceiveFileOnegate());
		
		// the person who receives the file from processing department
		XWPFRun receiverOnegate_column = content_row.getCell(8).getParagraphs().get(0).createRun();
		content_row.getCell(8).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(8).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		receiverOnegate_column.setFontSize(FONT_SIZE_10);
		receiverOnegate_column.setText(fileBean.getWhoReceiveFileOnegate());

		// the date that returns file to sender (for planing)
		XWPFRun outputDate_column = content_row.getCell(9).getParagraphs().get(0).createRun();
		content_row.getCell(9).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(9).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		outputDate_column.setFontSize(FONT_SIZE_10);
		outputDate_column.setText(fileBean.getOutputDate());

		// the date that sender receives the file
		XWPFRun getResultDate_column = content_row.getCell(10).getParagraphs().get(0).createRun();
		content_row.getCell(10).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(10).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		getResultDate_column.setFontSize(FONT_SIZE_10);
		getResultDate_column.setText(fileBean.getReturnDate());

		// Person gets the file that has approved by agent 
		XWPFRun personGetResult_column = content_row.getCell(11).getParagraphs().get(0).createRun();
		content_row.getCell(11).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(11).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		personGetResult_column.setFontSize(FONT_SIZE_10);
		personGetResult_column.setText(fileBean.getWhoGetResult());

		// The processing result of file
		XWPFRun result_column = content_row.getCell(12).getParagraphs().get(0).createRun();
		content_row.getCell(12).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(12).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		result_column.setFontSize(FONT_SIZE_10);
		result_column.setText(fileBean.getResult());
		
		// The processing result of file
		XWPFRun note_column = content_row.getCell(13).getParagraphs().get(0).createRun();
		content_row.getCell(13).getParagraphs().get(0).setSpacingAfter(SPACING_0);
		content_row.getCell(13).getParagraphs().get(0).setSpacingBefore(SPACING_06);
		note_column.setFontSize(FONT_SIZE_10);
		note_column.setText(fileBean.getNote());

	}
	
	private void createTableHeader(XWPFTableRow header_row){
		String[] headerTitle = {"T\u00EAn c\u00E1 nh\u00E2n/ c\u01A1 quan",
				"\u0110\u1ECBa \u0111i\u1EC3m",
				"Lo\u1EA1i th\u1EE7 t\u1EE5c",
				"Ng\u00E0y n\u1ED9p",
				"Ng\u00E0y nh\u1EADn (ph\u00F2ng CM)",
				"Ng\u01B0\u1EDDi nh\u1EADn (ph\u00F2ng CM)",
				"Ng\u00E0y nh\u1EADn (Ph\u00F2ng 1 c\u1EEDa)",
				"Ng\u01B0\u1EDDi nh\u1EADn (Ph\u00F2ng 1 c\u1EEDa)",
				"Ng\u00E0y h\u1EB9n tr\u1EA3",
				"Ng\u00E0y tr\u1EA3",
				"Ng\u01B0\u1EDDi nh\u1EADn",
				"K\u1EBFt qu\u1EA3 gi\u1EA3i quy\u1EBFt",
				"Ghi ch\u00FA"};

		XWPFRun stt_column = header_row.getCell(0).getParagraphs().get(0).createRun();
		stt_column.setText("STT");
		stt_column.setBold(true);
		stt_column.setFontFamily(FONT_TIMES_NEW_ROMAN); 
		stt_column.setFontSize(FONT_SIZE_10);
		
		for (int i = 0; i < headerTitle.length; i++) {
			
			header_row.createCell();
			XWPFRun header_colum = header_row.getCell(i+1).getParagraphs().get(0).createRun();
			header_row.getCell(i+1).getParagraphs().get(0).setAlignment(CENTER_ALIGN);
			header_colum.setText(headerTitle[i]);
			header_colum.setBold(true);
			header_colum.setFontFamily(FONT_TIMES_NEW_ROMAN); 
			header_colum.setFontSize(FONT_SIZE_10);

		}
	}
}
